package com.ruoyi.controller.achievements;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.achievements.*;
import com.ruoyi.util.BaseResult;
import com.ruoyi.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zeo
 * @explain 考核打分
 * @date 2020/8/24 16:20
 */
@Controller
@RequestMapping("/acvScoringManage")
public class AcvScoringManageController extends BaseController {

    @Autowired
    AcvScoringManageService acvScoringManageService;

    @Autowired
    AcvDepManageService acvDepManageService;

    @Autowired
    AcvTemplateManageService acvTemplateManageService;

    @Autowired
    AcvEmpManageService acvEmpManageService;

    @Autowired
    AcvParameterService acvParameterService;
    private String prefix = "achievements/scoringmanage";

    @GetMapping()
    public String acvScoringManage(ModelMap mmp) {
        mmp.put("depList", acvDepManageService.finDepList(new HashMap()));
        mmp.put("jobList", acvDepManageService.findJobList());
        return prefix + "/acvScoringManage";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map) {
        map.put("table", "acv_scoring");
        return getDataTable(acvScoringManageService.list(map));
    }

    /**
     * 验证是否已经引入
     *
     * @return
     */
    @RequestMapping("/validationIntroduction")
    @ResponseBody
    public BaseResult validationIntroduction() {
        if (acvScoringManageService.validationIntroduction() > 0) {
            return BaseResult.build(ResultEnum.FAILURE);
        } else {
            //顺便将配置日期带过去
            return BaseResult.build(ResultEnum.SUCCESS,acvParameterService.findParameter("指定考核日期"));
        }
    }

    @RequestMapping("/introduce")
    @ResponseBody
    public AjaxResult introduce(String years, String months) {
        //查询是否选择的日期是否已经提交
        if (acvScoringManageService.isSubmitByYearsAndMonths(years, months) == 0) {
            //插入被考核人信息
            if (acvScoringManageService.introduce(years, months) > 0) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error("引入失败,检查是否已经配置考核人员");
            }
        } else {
            return AjaxResult.error(years + "年" + months + "月的考核数据已提交,请核实!");
        }
    }

    @GetMapping("/toScoring")
    public String toScoring(String examineeId, String years, String months, String scoringId, String remarksTotal, String scoreTotal, String table, ModelMap mmap) {
        mmap.put("examineeObj", acvEmpManageService.findExamineeByUserId(examineeId));
        mmap.put("years", years);
        mmap.put("months", months);
        mmap.put("scoringId", scoringId);
        mmap.put("remarksTotal", remarksTotal);
        mmap.put("scoreTotal", scoreTotal);
        mmap.put("table", table);
        return prefix + "/scoring";
    }

    @RequestMapping("/scoringListByScoringId")
    @ResponseBody
    public TableDataInfo scoringListByScoringId(String scoringId, String type) {
        return getDataTable(acvScoringManageService.findScoringItems(scoringId, type));
    }

    @RequestMapping("/dataSave")
    @ResponseBody
    public BaseResult dataSave(String scoreArray, String totalScore, String scoringId, String remarksTotal,String table) {
        if (acvScoringManageService.dataSave(scoreArray, scoringId, totalScore, remarksTotal,table) > 0) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(ResultEnum.FAILURE);
        }
    }

    @RequestMapping("/submitData")
    @ResponseBody
    public AjaxResult submitData() {
        return acvScoringManageService.submitData();
    }

    @RequestMapping("/emptyData")
    @ResponseBody
    public AjaxResult emptyData() {
        return acvScoringManageService.emptyData();
    }

    @RequestMapping("/refreshPersonnel")
    @ResponseBody
    public AjaxResult refreshPersonnel(){
        return acvScoringManageService.refreshPersonnel();
    }
}
